home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_src.lha / LEDA-3.1c-source / src / vga.old / _vgalib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-17  |  10.6 KB  |  395 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  _vgalib.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16. #include "vgalib.h"
  17.  
  18. /* 
  19.  * setting the VGA into 640x480x16 color graphics mode or text mode
  20.  * (part of this code is based on "VGAlib" by Tommy Frandsen)
  21.  */
  22.  
  23. VIDEO_PTR  VIDEO=0;
  24.  
  25. int DISP_WIDTH = 640;
  26. int DISP_MAX_X = 639;
  27. int DISP_HEIGHT= 480;
  28. int DISP_MAX_Y = 479;
  29. int DISP_DEPTH = 4;
  30.  
  31. int LINE_BYTES = 80;
  32.  
  33. #define FONT_SIZE  0x2000
  34.  
  35. /* VGA index register ports */
  36. #define CRT_IC  0x3D4   /* CRT Controller Index - color emulation */
  37. #define CRT_IM  0x3B4   /* CRT Controller Index - mono emulation */
  38. #define ATT_IW  0x3C0   /* Attribute Controller Index & Data Write Register */
  39. #define GRA_I   0x3CE   /* Graphics Controller Index */
  40. #define SEQ_I   0x3C4   /* Sequencer Index */
  41. #define PEL_IW  0x3C8   /* PEL Write Index */
  42. #define PEL_IR  0x3C7   /* PEL Read Index */
  43.  
  44. /* VGA data register ports */
  45. #define CRT_DC  0x3D5   /* CRT Controller Data Register - color emulation */
  46. #define CRT_DM  0x3B5   /* CRT Controller Data Register - mono emulation */
  47. #define ATT_R   0x3C1   /* Attribute Controller Data Read Register */
  48. #define GRA_D   0x3CF   /* Graphics Controller Data Register */
  49. #define SEQ_D   0x3C5   /* Sequencer Data Register */
  50. #define MIS_R   0x3CC   /* Misc Output Read Register */
  51. #define MIS_W   0x3C2   /* Misc Output Write Register */
  52. #define IS1_RC  0x3DA   /* Input Status Register 1 - color emulation */
  53. #define IS1_RM  0x3BA   /* Input Status Register 1 - mono emulation */
  54. #define PEL_D   0x3C9   /* PEL Data Register */
  55.  
  56. /* VGA indexes max counts */
  57. #define CRT_C   24      /* 24 CRT Controller Registers */
  58. #define ATT_C   21      /* 21 Attribute Controller Registers */
  59. #define GRA_C   9       /* 9  Graphics Controller Registers */
  60. #define SEQ_C   5       /* 5  Sequencer Registers */
  61. #define MIS_C   1       /* 1  Misc Output Register */
  62.  
  63. /* VGA registers saving indexes */
  64. #define CRT     0               /* CRT Controller Registers start */
  65. #define ATT     CRT+CRT_C       /* Attribute Controller Registers start */
  66. #define GRA     ATT+ATT_C       /* Graphics Controller Registers start */
  67. #define SEQ     GRA+GRA_C       /* Sequencer Registers */
  68. #define MIS     SEQ+SEQ_C       /* General Registers */
  69. #define END     MIS+MIS_C       /* last */
  70.  
  71.  
  72. /* variables used to shift between monchrome and color emulation */
  73. static int CRT_I;        /* current CRT index register address */
  74. static int CRT_D;        /* current CRT data register address */
  75. static int IS1_R;        /* current input status register address */
  76. static int color_text;        /* true if color text emulation */
  77.  
  78.  
  79. /* BIOS mode 12h - 640x480x16 */
  80. static char g640x480x16_regs[60] = {
  81.   0x5F,0x4F,0x50,0x82,0x54,0x80,0x0B,0x3E,0x00,0x40,0x00,0x00,
  82.   0x00,0x00,0x00,0x00,0xEA,0x8C,0xDF,0x28,0x00,0xE7,0x04,0xE3,
  83.   0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
  84.   0x0C,0x0D,0x0E,0x0F,0x01,0x00,0x0F,0x00,0x00,
  85.   0x00,0x0F,0x00,0x20,0x00,0x00,0x05,0x0F,0xFF,
  86.   0x03,0x01,0x0F,0x00,0x06,
  87.   0xE3
  88. };
  89.  
  90.  
  91. /* default RGB values */
  92.  
  93. //                            white black red  green blue  yellow violet orange
  94. //                            cyan  brown pink green grey1 grey2  grey3  grey4
  95. static char RGB_RED[16]  = {  63,   0,    63,   0,   0,    54,    42,    54,   
  96.                                0,  48,    63,   9,   45,   40,    35,     0};
  97.  
  98. static char RGB_GREEN[16]= {  63,   0,     0,  54,    0,   54,    21,    36,  
  99.                               63,  28,    26,  35,   45,   40,    35,     0};
  100.  
  101. static char RGB_BLUE[16] = {  63,   0,     0,   0,   63,   18,    63,     0, 
  102.                               63,  18,    47,   9,   45,   40,    35,     0};
  103.  
  104.  
  105. static char text_regs[60];   /* VGA registers for saved text mode */
  106.  
  107. /* saved text mode palette values */
  108. static char text_red[256];
  109. static char text_green[256];
  110. static char text_blue[256];
  111.  
  112. /* saved graphics mode palette values */
  113. //static char graph_red[256];
  114. //static char graph_green[256];
  115. //static char graph_blue[256];
  116.  
  117. static int initialized = 0;
  118.  
  119. static char font_buf1[FONT_SIZE];  /* saved font data - plane 2 */
  120. static char font_buf2[FONT_SIZE];  /* saved font data - plane 3 */
  121.  
  122.  
  123. static void set_regs(char regs[])
  124. {
  125.     int i;
  126.  
  127.     /* disable video */
  128.     port_in(IS1_R);    
  129.     port_out(0x00, ATT_IW);
  130.  
  131.     /* update misc output register */
  132.     port_out(regs[MIS], MIS_W);
  133.  
  134.     /* synchronous reset on */
  135.     port_out(0x00,SEQ_I);
  136.     port_out(0x01,SEQ_D);    
  137.  
  138.     /* write sequencer registers */
  139.     for (i = 1; i < SEQ_C; i++) {
  140.     port_out(i, SEQ_I);
  141.     port_out(regs[SEQ+i], SEQ_D);
  142.     }
  143.  
  144.     /* synchronous reset off */
  145.     port_out(0x00, SEQ_I);
  146.     port_out(0x03, SEQ_D);    
  147.  
  148.     /* deprotect CRT registers 0-7 */
  149.     port_out(0x11, CRT_I);        
  150.     port_out(port_in(CRT_D)&0x7F, CRT_D);
  151.  
  152.     /* write CRT registers */
  153.     for (i = 0; i < CRT_C; i++) {
  154.     port_out(i, CRT_I);
  155.     port_out(regs[CRT+i], CRT_D);
  156.     }
  157.  
  158.     /* write graphics controller registers */
  159.     for (i = 0; i < GRA_C; i++) {
  160.     port_out(i, GRA_I);
  161.     port_out(regs[GRA+i], GRA_D);
  162.     }
  163.  
  164.     /* write attribute controller registers */
  165.     for (i = 0; i < ATT_C; i++) {
  166.     port_in(IS1_R);   /* reset flip-flop */
  167.     port_out(i, ATT_IW);
  168.     port_out(regs[ATT+i],ATT_IW);
  169.     }
  170. }
  171.  
  172.  
  173. static void vga_initialize()
  174. {
  175.     int  i, j;
  176.  
  177.     if (initialized) return;
  178.  
  179.     initialized = 1;
  180.  
  181. #if defined(__GNUG__)
  182.     VIDEO = (VIDEO_PTR)0xd0000000;
  183. #else
  184. #if defined(__ZTC__) && defined(DOS386)
  185.     VIDEO = (VIDEO_PTR)_x386_mk_protected_ptr(0xa0000);
  186. #else
  187.     VIDEO = (VIDEO_PTR)MK_FP(0xa000,0);
  188. #endif
  189. #endif
  190.  
  191.  
  192.     /* color or monochrome text emulation? */
  193.     color_text = port_in(MIS_R)&0x01;
  194.  
  195.     /* chose registers for color/monochrome emulation */
  196.     if (color_text) {
  197.     CRT_I = CRT_IC;
  198.     CRT_D = CRT_DC;
  199.     IS1_R = IS1_RC;
  200.     } else {
  201.     CRT_I = CRT_IM;
  202.     CRT_D = CRT_DM;
  203.     IS1_R = IS1_RM;
  204.     }
  205.  
  206.     /* disable video */
  207.     port_in(IS1_R);    
  208.     port_out(0x00, ATT_IW);
  209.  
  210.     /* save text mode palette - first select palette index 0 */
  211.     port_out(0, PEL_IR);
  212.  
  213.     /* read RGB components - index is autoincremented */
  214.     for(i = 0; i < 256; i++) {
  215.     for(j = 0; j < 10; j++) ;   /* delay (minimum 240ns) */
  216.     text_red[i] = port_in(PEL_D);
  217.     for(j = 0; j < 10; j++) ;   /* delay (minimum 240ns) */
  218.     text_green[i] = port_in(PEL_D);
  219.     for(j = 0; j < 10; j++) ;   /* delay (minimum 240ns) */
  220.     text_blue[i] = port_in(PEL_D);
  221.     }
  222.  
  223.     /* save text mode VGA registers */
  224.     for (i = 0; i < CRT_C; i++) {
  225.      port_out(i, CRT_I);
  226.      text_regs[CRT+i] = port_in(CRT_D);
  227.     }
  228.     for (i = 0; i < ATT_C; i++) {
  229.            port_in(IS1_R);
  230.          port_out(i, ATT_IW);
  231.          text_regs[ATT+i] = port_in(ATT_R);
  232.     }
  233.     for (i = 0; i < GRA_C; i++) {
  234.             port_out(i, GRA_I);
  235.             text_regs[GRA+i] = port_in(GRA_D);
  236.     }
  237.     for (i = 0; i < SEQ_C; i++) {
  238.             port_out(i, SEQ_I);
  239.             text_regs[SEQ+i] = port_in(SEQ_D);
  240.     }
  241.     text_regs[MIS] = port_in(MIS_R);
  242.  
  243.     /* shift to color emulation */
  244.     CRT_I = CRT_IC;
  245.     CRT_D = CRT_DC;
  246.     IS1_R = IS1_RC;
  247.     port_out(port_in(MIS_R)|0x01, MIS_W);
  248.  
  249.     /* save font data - first select a 16 color graphics mode */
  250.     set_regs(g640x480x16_regs);
  251.  
  252.     /* save font data in plane 2 */
  253.     port_out(0x04, GRA_I);
  254.     port_out(0x02, GRA_D);
  255.     for(i = 0; i < FONT_SIZE; i++) font_buf1[i] = VIDEO[i];
  256.  
  257.     /* save font data in plane 3 */
  258.     port_out(0x04, GRA_I);
  259.     port_out(0x03, GRA_D);
  260.     for(i = 0; i < FONT_SIZE; i++) font_buf2[i] = VIDEO[i];
  261. }
  262.  
  263.  
  264. void set_palette(int index, int red, int green, int blue)
  265. {
  266.     int i;
  267.  
  268.     /* select palette register */
  269.     port_out(index, PEL_IW);
  270.  
  271.     /* write RGB components */
  272.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  273.     port_out(red, PEL_D);
  274.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  275.     port_out(green, PEL_D);
  276.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  277.     port_out(blue, PEL_D);
  278. }
  279.  
  280.  
  281. void get_palette(int index, int *red, int *green, int *blue)
  282. {
  283.     int i;
  284.  
  285.     /* select palette register */
  286.     port_out(index, PEL_IR);
  287.  
  288.     /* read RGB components */
  289.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  290.     *red = (int) port_in(PEL_D);
  291.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  292.     *green = (int) port_in(PEL_D);
  293.     for(i = 0; i < 10; i++) ;   /* delay (minimum 240ns) */
  294.     *blue = (int) port_in(PEL_D);
  295. }
  296.  
  297. static void vga_clear(int c)
  298. {
  299.   register VIDEO_PTR p;
  300.   register VIDEO_PTR last;
  301.  
  302.   /* set color c */
  303.   port_out(c, GRA_I );
  304.   port_out(0, GRA_D );
  305.  
  306.   /* set mode 0 */
  307.   port_out(0x03, GRA_I );
  308.   port_out(0, GRA_D );
  309.  
  310.   /* write to all bits */
  311.   port_out(0x08, GRA_I );
  312.   port_out(0xFF, GRA_D );
  313.  
  314.   last  = VIDEO + DISP_HEIGHT*LINE_BYTES;
  315.  
  316.   for(p = VIDEO; p < last; p++)  *p = 0;
  317.  
  318. }
  319.  
  320.  
  321. void vga_init(int mode, int root_col)  // mode = 0: Text, 1: 640x480x16 
  322. {
  323.     int i;
  324.  
  325.     vga_initialize();
  326.     
  327.     if (mode == 0)  // TEXT
  328.       { 
  329.         /* restore font data - first select a 16 color graphics mode */
  330.         set_regs(g640x480x16_regs);
  331.  
  332.     /* disable Set/Reset Register */
  333.         port_out(0x01, GRA_I );
  334.         port_out(0x00, GRA_D );
  335.  
  336.         /* restore font data in plane 2 - necessary for all VGA's */
  337.         port_out(0x02, SEQ_I );
  338.         port_out(0x04, SEQ_D );
  339.         for(i = 0; i < FONT_SIZE; i++) VIDEO[i] = font_buf1[i];
  340.  
  341.         /* restore font data in plane 3 - necessary for Trident VGA's */
  342.         port_out(0x02, SEQ_I );
  343.         port_out(0x08, SEQ_D );
  344.         for(i = 0; i < FONT_SIZE; i++) VIDEO[i] = font_buf2[i];
  345.  
  346.         /* change register adresses if monochrome text mode */
  347.         if (!color_text) {
  348.             CRT_I = CRT_IM;
  349.             CRT_D = CRT_DM;
  350.             IS1_R = IS1_RM;
  351.             port_out(port_in(MIS_R)&0xFE, MIS_W);
  352.         }
  353.  
  354.     /* restore text mode VGA registers */
  355.         set_regs(text_regs);
  356.  
  357.         /* restore saved palette */
  358.         for(i = 0; i < 256; i++)
  359.             set_palette(i, text_red[i], text_green[i], text_blue[i]);
  360.  
  361.         DISP_WIDTH = 80;
  362.         DISP_MAX_X = 79;
  363.         DISP_HEIGHT= 25;
  364.         DISP_MAX_Y = 24;
  365.       }
  366.     else // graphics mode
  367.       { 
  368.         /* shift to color emulation */
  369.         CRT_I = CRT_IC;
  370.         CRT_D = CRT_DC;
  371.         IS1_R = IS1_RC;
  372.         port_out(port_in(MIS_R)|0x01, MIS_W);
  373.         set_regs(g640x480x16_regs);
  374.  
  375.         /* set default palette */
  376.         for(i = 0; i < 16; i++)
  377.           set_palette(i, RGB_RED[i], RGB_GREEN[i], RGB_BLUE[i]);
  378.  
  379.         vga_clear(root_col);
  380.  
  381.         LINE_BYTES = 80;
  382.         DISP_WIDTH = 640;
  383.         DISP_MAX_X = 639;
  384.         DISP_HEIGHT= 480;
  385.         DISP_MAX_Y = 479;
  386.         DISP_DEPTH = 4;
  387.       }
  388.  
  389.     /* enable video */
  390.     port_in(IS1_R);
  391.     port_out(0x20, ATT_IW);
  392.  
  393. }
  394.